<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>主页</title>
    <link rel="stylesheet" href="./assets/index.css" />
    <style>
        #app {
            width: 1024px;
            margin: 0 auto;
        }
        .el-header {
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .header-right-dl {
            position: absolute;
            top: 50%;
            right: 0;
            transform: translateY(-50%);
        }
        .title-dl {
            text-align: center;
            display: flex;
            justify-content: center;
            margin-bottom: 10px;
        }
        .item-dl {
            flex-shrink: 0;
            margin: 0 10px;
            padding: 5px 10px;
        }
        .item-hover-dl {
            cursor: pointer;
            transition: all 0.3s linear;
            border-radius: 4px;
        }
        .item-hover-dl:hover {
            background-color: #ccc;
        }
        .act-dl {
            background-color: #ccc;
        }

        .film-list-dl {
            display: grid;
            grid-template: repeat(2, 1fr)/ repeat(5, 1fr);
            gap: 20px 0;
        }
        .box-dl {
            display: inline-block;
            /* width: fit-content; */
            width: 115px;
        }
        .cover-dl {
            width: 115px;
            height: 161px;
            background-color: #eee;
            position: relative;
        }
        .cover-dl img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        .cover-dl .img-holder-dl {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        .title-box-dl {
            text-align: center;
            overflow: hidden;
        }
        .title-box-dl .rating-dl {
            color: #e49073;
        }
        .title-box-dl a {
            display: inline-block;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
            width: 115px;
            text-decoration: none;
        }

        .page-menu-list-dl {
            width: 100px;
            display: flex;
            justify-content: space-between;
            margin: 0 auto;
        }
        .page-menu-list-dl svg {
            cursor: pointer;
        }
        .page-menu-list-dl svg:hover {
            color: aqua;
        }
    </style>
</head>
<body>
    <div id="app">
        <el-container>
            <el-header>

                <div style="width: 300px;">
                    <div class="title-dl">
                        <span class="item-dl item-hover-dl" :class="tab==1?'act-dl':''" @click="doChangTab(1)">热映电影</span>
                        <span class="item-dl">|</span>
                        <span class="item-dl item-hover-dl" :class="tab==2?'act-dl':''" @click="doChangTab(2)" >电影搜索</span>
                    </div>
                    <el-input v-model.trim="keyWords" placeholder="电影关键字" v-show="tab == 2" >
                        <template #append>
                            <el-button @click="doSearchFilm">
                                <el-icon>
                                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" ><path fill="currentColor" d="m795.904 750.72 124.992 124.928a32 32 0 0 1-45.248 45.248L750.656 795.904a416 416 0 1 1 45.248-45.248zM480 832a352 352 0 1 0 0-704 352 352 0 0 0 0 704"></path></svg>
                                </el-icon>
                            </el-button>
                        </template>
                    </el-input>
                </div>

                <div class="header-right-dl" >
                    <template v-if="loginFlag">
                        <el-link type="primary" href="./user.html">
                            <span>欢迎:</span>
                            <span>{{login.loginName}}</span>
                            <span style="display: inline-block;margin-right: 10px;" ></span>
                        </el-link>
                        <el-button type="info" plain @click="doLogOut" size="small">注销</el-button>
                    </template>
                    <el-link v-else type="warning" href="./login.html">去登录</el-link>
                </div>
            </el-header>
            <el-main>

                <div class="film-list-dl" v-loading="loading">
                    <div class="box-dl" v-for="f in items">
                        <div class="cover-dl">
                            <img v-show="f.cover.loaded" :src="f.cover.url" alt="no cover" @loadstart="f.cover.loaded=false" @load="f.cover.loaded=true;console.log(f.id)">
                            <div v-show="!f.cover.loaded" class="img-holder-dl">Loading</div>
                        </div>
                        <div class="title-box-dl">
                            <a :href="'./film.html?filmId=' + f.id">{{f.title}}</a>
                            <span class="rating-dl" >{{f.rating?.value}}</span>
                        </div>
                    </div>
                </div>

                <div style="margin-top: 20px;" v-show="tab == 1" >
                    <div class="page-menu-list-dl">
                        <el-icon :size="20" @click="doPage(-1)" >
                            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" ><path fill="currentColor" d="M685.248 104.704a64 64 0 0 1 0 90.496L368.448 512l316.8 316.8a64 64 0 0 1-90.496 90.496L232.704 557.248a64 64 0 0 1 0-90.496l362.048-362.048a64 64 0 0 1 90.496 0z"></path></svg>
                        </el-icon>
                        <el-icon :size="20" @click="doPage(1)" >
                            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" ><path fill="currentColor" d="M338.752 104.704a64 64 0 0 0 0 90.496l316.8 316.8-316.8 316.8a64 64 0 0 0 90.496 90.496l362.048-362.048a64 64 0 0 0 0-90.496L429.248 104.704a64 64 0 0 0-90.496 0z"></path></svg>
                        </el-icon>
                    </div>
                </div>

            </el-main>
        </el-container>
    </div>
    <script src="./assets/vue.global.prod.js"></script>
    <script src="./assets/index.full.js"></script>
    <script src="./assets/index.iife.min.js"></script>
    <script src="./assets/axios.min.js"></script>
    <script src="./assets/film.js"></script>
    <script>
        const { createApp } = Vue;
        let app = createApp({
            data() {
                return {
                    login: {},
                    loginFlag: false,
                    start: 0,
                    count: 10,
                    items: [],
                    tempItems: [],
                    total: 0,
                    loading: false,
                    keyWords: '',
                    tab: 1,
                }
            },
            methods: {
                async doLoadMovieShowing() {
                    this.loading = true;
                    // /film/movieShowing
                    try {
                        const resp = await axios.get('/film/movieShowing?start='+ this.start +'&count='+this.count);
                        let result = resp.data;
                        //console.log(result);
                        let pageObj = result.data;
                        //数据
                        let items = pageObj.subject_collection_items;
                        this.total = pageObj.total;
                        console.log('films:', items);

                        //清空旧数据
                        this.items = [];
                        //存入 vue data
                        for(let i=0;i<items.length;i++){
                            let f = items[i];
                            f.cover.loaded = false;
                            this.items.push(f);
                        }

                    } catch (error) {
                        console.log('error:', error);
                    }
                    this.loading = false;
                },

                doPage(n) {
                    let start = this.start;
                    start = start + n * this.count;
                    if (start < 0) {
                        this.$message.warning('当前已是第一页');
                        return;
                    }
                    if (start > this.total - 1) {
                        this.$message.warning('已展示全部数据');
                        return;
                    }
                    this.start = start;
                    this.doLoadMovieShowing();
                },

                doLogOut() {
                    localStorage.removeItem('token');
                    localStorage.removeItem('login');
                    this.login = {};
                    this.loginFlag = false;
                    this.$message.success('成功退出');
                },

                async doSearchFilm() {
                    if (this.keyWords.length <= 0) {
                        this.$message.warning('电影关键字不能为空');
                        return;
                    }
                    try {
                        const resp = await axios.request({
                            url: '/film/search',
                            method: 'get',
                            params: {
                                q: this.keyWords
                            }
                        });
                        let result = resp.data;
                        console.log(result);
                        if (result.success) {
                            this.items = [];
                            let arr = result.data.subjects.items;
                            for(let i=0; i<arr.length; i++) {
                                let film = arr[i].target;
                                //处理数据

                                film.cover = {
                                    loaded: true,
                                    url: film.cover_url
                                };

                                this.items.push(film);
                            }
                        } else {
                            this.$message.error(result.msg);
                        }
                    } catch (error) {
                        console.log('error:', error);
                    }
                },

                doChangTab(t) {
                    this.tab = t;
                    if (t == 2) {
                        this.tempItems = this.items;
                        this.items = [];
                    } else {
                        this.items = this.tempItems;
                        this.tempItems = [];
                    }
                }

            },
            mounted() {
                this.doLoadMovieShowing();

                let str = localStorage.getItem('login');
                if (null == str) {
                    this.$message.error('您还未登录');
                    return;
                }
                console.log('login:', str);
                // JSON.parse 把 string 转换为 js对象
                let login = JSON.parse(str);
                this.login = login;
                this.loginFlag = true;
            }
        });
        app.use(ElementPlus);
        for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
            app.component(key, component);
        }
        app.mount('#app');
    </script>
</body>
</html>